package data;
import haxe.Template;
import data.HxTemplate;
import js.Cart;
import js.Cookie;
import js.Dom;
import js.Lib;

using me.cunity.tools.MathTools;
using StringTools;
/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */

 class ShopPage implements Dynamic
 {
	public var currency:String;
	public var currencySymbol:String;
	public var vatName:String;
	public var vatIncluded:Bool;
	public var groups:List<Group>;
	
	public function new()
	{
		groups = new List();
	}
	
	public function float2String(f:Float, precision:Int = 2):String 
	{
		var s:String = Std.string(f).replace('.', ',');
		var decPoint = s.indexOf(',');
		//trace(s + ' length:' + s.length + ' decPoint:' + decPoint + ' precision:' + precision);
		switch ( decPoint)
		{
			case  -1:
			s += ',00';
			default:
			s = s.rpad('0', (precision - (s.length - 1 - decPoint) + s.length ));
		}
		return s;
	}	
 }
 
class ShopLoader 
{
	var cart:Cart;
	var groups:List<Group>;
	public var shopPage:ShopPage;
	var win:Window;
	
	public function new(win:Window) 
	{
		this.win = win;
		cart = win.cart;
	}
	
	public function create(win:Window, contentBox:HtmlDom)
	{
		var xData:Xml = this.win.xContent;
		shopPage = new ShopPage();
		
		for (a in xData.firstElement().attributes())
		{
			Reflect.setField(shopPage, a, xData.firstElement().get(a));
		}
		
		for (g in xData.firstElement().elementsNamed('Group'))
			shopPage.groups.add(new Group(g));
		//trace(Std.string(shopPage));
		var t:HxTemplate = new HxTemplate(haxe.Resource.getString("shopContent"));
	
		contentBox.innerHTML = '<center>' + t.execute(shopPage, { itemData:itemData } ) + '</center>';
		
		var shopNav:HtmlDom = Lib.window.document.getElementById('shopNav');
		var shopNavContent:String = '';

		for (g in shopPage.groups)
		{
			var itemName:String = '';
			for (i in g.items)
			{
				if (i.title != itemName)
				{
					itemName = i.title;
					//trace('itemName:' + itemName);
					shopNavContent += '<div class="navLeftItem" onclick="javascript:shopScrollIntoView(\'' +
						itemName+'\')"><a href = "#"> ' + itemName +'</a></div>';
				}
				cart.items.set(i.id, i);
			}
		}		
	}
	
	function itemData(resolve:String->Dynamic, s:String, item:Item)
	{
		var dataTable:String = '';
		//trace(Std.string(item));
		
		for (key in item.dataMap.keys())
		{
			var rowData:Dynamic = item;
			rowData.currencySymbol = shopPage.currencySymbol;

			switch (key)
			{
				case 'basePrice':
				switch(item.volumeUnit)
				{
					case 'g':
					rowData.value =  shopPage.float2String((item.price * 1000 / item.volume).round2(2));
					//trace( ' item.price:' + item.price + ' item.volume:' + item.volume + ' rowData.value:' + rowData.value);
					default:
					continue;
				}
				
				case 'price':
				//rowData.value =  Std.string(item.price.round2(2)).replace('.', ',');
				rowData.value = shopPage.float2String( item.price.round2(2));				
				//trace('price:' + rowData.value);
				
				default:
				rowData.value = Reflect.field(item, key);
				
			}
			
			var rowDataMap:Xml = item.dataMap.get(key);
			for (a in rowDataMap.attributes())
			{
				var att:String = rowDataMap.get(a);
				Reflect.setField(rowData, a, (att.indexOf('::')>-1) ? new Template(att).execute(item) : att);
			}			
			//trace(Std.string(rowData));
			/*var rT:Template = new Template(
			'<tr><td class="::class::">::label::</td><td class="::class:: right">::value::</td><td class="::class::">::suffix::</td></tr>');
			var res:String = rT.execute(rowData);
			//trace(res);*/
			dataTable += new Template(
			'<tr><td class="::class::">::label::</td><td class="::class:: right">::value::</td><td class="::class::">::suffix::</td></tr>').execute(rowData);
										/*::foreach dataRows::
								<tr><td>::dataName::</td><td>::dataValue::</td><td>::dataUnit::</td></tr>
								::end::*/
		}
		return dataTable;
	}	
	
}